//
//  WTSquareCell.m
//  OneDay
//
//  Created by lanou on 16/1/15.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTSquareCell.h"
#import <UIImageView+WebCache.h>


#define kMargin 10 * kRelativeLength_X

#define kTitleLabelHeight (20 * kRelativelength_Y)

#define kPicOneImageVHeight (70 * kRelativelength_Y)
@implementation WTSquareCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 标题
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kMargin, kMargin, kScreenWidth - 2 * kMargin, kTitleLabelHeight)];
        [self.contentView addSubview:_titleLabel];
        
        // 内容
        _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(_titleLabel.left, _titleLabel.bottom + 5 * kRelativelength_Y, _titleLabel.width, 35 * kRelativelength_Y)];
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_contentLabel];
        
        
        // 图片
        CGFloat width = (kScreenWidth - 80 * kRelativeLength_X - 3 * kMargin) / 3;
        _picOneImageV = [[UIImageView alloc]initWithFrame:CGRectMake(kMargin, _contentLabel.bottom + 5 * kRelativelength_Y, width, kPicOneImageVHeight)];
        _picOneImageV.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_picOneImageV];
        
        
        _picTwoImageV = [[UIImageView alloc]initWithFrame:CGRectMake(_picOneImageV.right + kMargin, _picOneImageV.top, width, kPicOneImageVHeight)];
        _picTwoImageV.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_picTwoImageV];
        
        _picThrImagV = [[UIImageView alloc]initWithFrame:CGRectMake(_picTwoImageV.right + kMargin, _picOneImageV.top, width, kPicOneImageVHeight)];
        _picThrImagV.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_picThrImagV];
        
        _picNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(_picThrImagV.width - 40 * kRelativeLength_X, _picThrImagV.height - 20 * kRelativelength_Y, 40 *  kRelativeLength_X, 20 * kRelativelength_Y)];
        _picNumLabel.backgroundColor = [UIColor grayColor];
        _picNumLabel.textAlignment = NSTextAlignmentRight;
        _picNumLabel.textColor = [UIColor whiteColor];
        [_picThrImagV addSubview:_picNumLabel];
        
        // 作者
        _authorNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(kMargin, _picOneImageV.bottom + 5 * kRelativelength_Y, 100 * kRelativeLength_X, kTitleLabelHeight)];
        _authorNameLabel.font = [UIFont systemFontOfSize:14];
        _authorNameLabel.textColor = [UIColor blueColor];
        [self.contentView addSubview:_authorNameLabel];
        
        // 时间和类型
        _timeAgoLabel = [[UILabel alloc]initWithFrame:CGRectMake(_authorNameLabel.right + kMargin, _authorNameLabel.top, 250 * kRelativeLength_X, kTitleLabelHeight)];
        _timeAgoLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_timeAgoLabel];
        
        
        _replyNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - 80 * kRelativeLength_X - kMargin, _authorNameLabel.top, 80 * kRelativeLength_X, _authorNameLabel.height)];
        _replyNumLabel.font = [UIFont systemFontOfSize:14];
        _replyNumLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_replyNumLabel];
        

    }
    return self;
}


- (void)setModel:(WTSquareModel *)model
{
    _model = model;
    
    _titleLabel.text = model.title;
    _contentLabel.text = model.newsAbstract;
    

    //判断有没有图片, 自适应
    if ([model.picList count] == 0 ) {

        // 隐藏图片
        _picOneImageV.hidden = YES;
        _picTwoImageV.hidden = YES;
        _picThrImagV.hidden = YES;
     
        
        // 时间和类型
        
        // 作者
        _authorNameLabel.frame = CGRectMake(kMargin, _contentLabel.bottom + kMargin, 100 * kRelativeLength_X, kTitleLabelHeight);
        
        _timeAgoLabel.frame = CGRectMake(_authorNameLabel.right + kMargin, _authorNameLabel.top, 250 * kRelativeLength_X, kTitleLabelHeight);
       
        _replyNumLabel.frame = CGRectMake(kScreenWidth - 80 * kRelativeLength_X - kMargin, _authorNameLabel.top, 80 * kRelativeLength_X, _authorNameLabel.height);
               
    }else {
    
        if([model.picList count] == 1){
        [_picOneImageV sd_setImageWithURL:[NSURL URLWithString:model.picList[0]] placeholderImage:[UIImage imageNamed:@"m-zanweitu"]];
            _picOneImageV.hidden = NO;
            _picTwoImageV.hidden = YES;
            _picThrImagV.hidden = YES;
        }else if([model.picList count] == 2) {
          [_picOneImageV sd_setImageWithURL:[NSURL URLWithString:model.picList[0]] placeholderImage:[UIImage imageNamed:@"m-zanweitu"]];
          [_picTwoImageV sd_setImageWithURL:[NSURL URLWithString:model.picList[1]] placeholderImage:[UIImage imageNamed:@"m-zanweitu"]];
            _picTwoImageV.hidden = NO;
            _picOneImageV.hidden = NO;
            _picThrImagV.hidden = YES;
        }else {
            [_picOneImageV sd_setImageWithURL:[NSURL URLWithString:model.picList[0]] placeholderImage:[UIImage imageNamed:@"m-zanweitu"]];
            [_picTwoImageV sd_setImageWithURL:[NSURL URLWithString:model.picList[1]] placeholderImage:[UIImage imageNamed:@"m-zanweitu"]];
            [_picThrImagV sd_setImageWithURL:[NSURL URLWithString:model.picList[2]] placeholderImage:[UIImage imageNamed:@"m-zanweitu"]];
            
            _replyNumLabel.text = [NSString stringWithFormat:@"%ld图",(unsigned long)[model.picList count]];
            _picTwoImageV.hidden = NO;
            _picOneImageV.hidden = NO;
            _picThrImagV.hidden = NO;
            _picNumLabel.text = [NSString stringWithFormat:@"%ld图",(unsigned long)[model.picList count]];
        }
        
        
        
        // 作者
        _authorNameLabel.frame = CGRectMake(kMargin, _picOneImageV.bottom + 5 * kRelativelength_Y, 100 * kRelativeLength_X, kTitleLabelHeight);
        // 时间和类型
        _timeAgoLabel.frame = CGRectMake(_authorNameLabel.right + kMargin, _authorNameLabel.top, 250 * kRelativeLength_X, kTitleLabelHeight);
        _replyNumLabel.frame = CGRectMake(kScreenWidth - 80 * kRelativeLength_X - kMargin, _authorNameLabel.top, 80 * kRelativeLength_X, _authorNameLabel.height);
    }

    _authorNameLabel.text = model.author;
    _timeAgoLabel.text = [NSString stringWithFormat:@"%@·%@",model.timeAgo, model.circleName];
    _replyNumLabel.text = [NSString stringWithFormat:@"%@评论",model.replies];
    
    
}
+ (CGFloat)heightForRowWithModel:(WTSquareModel *)model
{
    if ([model.picList count]!= 0) {
        return kMargin + kTitleLabelHeight + 5 * kRelativelength_Y + 35 * kRelativelength_Y + 5 * kRelativelength_Y + kPicOneImageVHeight + 5 * kRelativelength_Y + kTitleLabelHeight+ kMargin;
    }else {
       return kMargin + kTitleLabelHeight + 5 * kRelativelength_Y + 35 * kRelativelength_Y + 10 * kRelativelength_Y + kTitleLabelHeight+ kMargin;
    }

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
